package ubiqweb.spring3.controller;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.jasper.tagplugins.jstl.core.Redirect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import ubiqweb.spring3.model.LoginDetails;
import ubiqweb.spring3.model.LoginForm;
import ubiqweb.spring3.service.LoginService;

@Controller
@SessionAttributes
@RequestMapping("/home")
public class HomeController {
	
	@Autowired
	private LoginService loginService;

	
//	 public ModelAndView home(HttpSession session, HttpServletRequest request) {
//		session.invalidate(); //destroy session
//		ModelAndView model = new ModelAndView("home");
//		model.addObject("message", "Hello World!");
//	    return model;
//	 }
	
	
    @SuppressWarnings("unchecked")
	@RequestMapping(method = RequestMethod.GET)
    public String showForm(Map model) {
            LoginForm loginForm = new LoginForm();
            model.put("loginForm", loginForm);
            return "home";
    }

    @SuppressWarnings("unchecked")
	@RequestMapping(method = RequestMethod.POST)
    public String processForm(@Valid LoginForm loginForm, BindingResult result,HttpSession session,
                    Map model) {
    		
    		loginForm = (LoginForm) model.get("loginForm");
    		List<LoginDetails> loginDetails = loginService.getListLoginDetails(loginForm.getUserName(), loginForm.getPassword());
    	
    		//validation for null fields
            if (result.hasErrors()) {
                    return "home";
            } else if (loginDetails.size() == 0) {
        		model.put("invalid", "Invalid username and password!");
                return "home";
            }
            
            //for whether entered credentials match to any of the records.
            
            if (!loginForm.getUserName().equals(loginDetails.get(0).getUserName())
                            || !loginForm.getPassword().equals(loginDetails.get(0).getPassword())) {
            	
            	return "home";
                    
            }
            
	        session.setAttribute("sessionUsrID", loginDetails.get(0).getUserId());
	        session.setAttribute("sessionUsrTyp", loginDetails.get(0).getUserType());
	        session.setAttribute("sessionUsrName", loginDetails.get(0).getUserName());
            model.put("loginForm", loginForm);
            return "home";
    }
   

}
